<?php
include_once( "common.php" );
checkLogin( STATUS_ADMIN );
outputHeader();
?>
<div class = "header">Add entry</div>
<form action = "addEntry.php" method = "post">
<table style = "width:600px;">
<tr>
<td>User</td><td><input type = "text" name = "user" /></td>
<td>Password</td><td><input type = "password" name = "password" /></td>
</tr>
<tr>
<td>Project</td><td><input type = "text" name = "project" /></td>
<td></td><td></td>
</tr>
<tr><td></td><td><input type = "submit" value = "Add entries" /></td></tr>
</table>
</form>
<div class = "header">Edit users</div>
<table class = "admin">
<tr>
<th style = "width:40px;">ID</th>
<th style = "width:300px;">User</th>
<th style = "width:70px;">Status</th>
<th style = "width:70px;">Assign</th>
<th style = "width:70px;">Restrict</th>
<th style = "width:200px;">Last seen</th>
</tr>
<?php
# Get all users
$result = db_query( "SELECT * FROM User ORDER BY User.status DESC, User.name" );

while ( $row = db_fetch_object( $result ) )
{
    # Check the user's status
    if ( $row->status == 2 )
        $row->status = "admin";
    else if ( $row->status == 1 )
        $row->status = "user";
    else if ( $row->status == 0 )
        $row->status = "guest";

    print "<tr style = 'background-color:#EEEEEE;'>\n";
    print "<td><a href = 'viewUser.php?id={$row->id}'>{$row->id}</a></td>\n";
    print "<td><a href = 'viewUser.php?id={$row->id}'>{$row->name}</a></td>\n";

    # See if the user is locked
    if ( $row->locked == 1 )
        print "<td style = 'background-color:#CC3333;'>{$row->status}</td>\n";
    else
        print "<td>{$row->status}</td>\n";

    # Check for open bugs
    print "<td>" . db_num_rows( db_query( "SELECT Bug.id FROM Bug, Status WHERE Bug.assigned = {$row->id} AND Bug.Status = Status.id AND ( Status.name = 'Open' OR Status.name = 'New' )" ) ) . "</td>\n";

    # Check for user restrictions
    print "<td>" . db_num_rows( db_query( "SELECT user FROM UserProject WHERE user = {$row->id}" ) ) . "</td>\n";

    $timestamp = formatTimestamp( $row->login );
    print "<td>{$timestamp}</td>\n";
    print "</tr>\n";
}

print "</table>\n";
?>
<div class = "header">Edit projects</div>
<table class = "admin">
<tr>
<th style = "width:40px;">ID</th>
<th style = "width:300px;">Project</th>
<th style = "width:70px;">Open</th>
<th style = "width:70px;">Closed</th>
<th style = "width:70px;">Total</th>
</tr>
<?php
# Get all projects
$result = db_query( "SELECT * FROM Project ORDER BY Project.name" );

while ( $row = db_fetch_object( $result ) )
{
    # Get bug counts
    $result_open = db_num_rows( db_query( "SELECT Bug.id FROM Bug, Status WHERE Bug.project = {$row->id} AND Bug.status = Status.id AND Status.name != 'Closed'" ) );
    $result_closed = db_num_rows( db_query( "SELECT Bug.id FROM Bug, Status WHERE Bug.project = {$row->id} AND Bug.status = Status.id AND Status.name = 'Closed'" ) );
    $result_all = $result_open + $result_closed;

    print "<tr style = 'background-color:#EEEEEE;'>\n";
    print "<td><a href = 'viewProject.php?id={$row->id}'>{$row->id}</a></td>\n";
    print "<td><a href = 'viewProject.php?id={$row->id}'>{$row->name}</a></td>\n";
    print "<td>{$result_open}</td>\n";
    print "<td>{$result_closed}</td>\n";
    print "<td>{$result_all}</td>\n";
    print "</tr>\n";
}

print "</table>\n";
outputFooter();
?>
